package com.hanbang.chu_xing_gao_zhao.view_find_esoterica;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.base.BaseActivity;
import com.hanbang.chu_xing_gao_zhao.utils.MyLog;
import com.hanbang.chu_xing_gao_zhao.utils.MyRequestCallBack;
import com.hanbang.chu_xing_gao_zhao.utils.MySharedPreferences;
import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.chu_xing_gao_zhao.view_public.VideoPlayActivity;
import com.hanbang.cxgz_2.R;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONObject;

public class FindEsotericaDetails_D extends BaseActivity {
    private TextView date;
    /**
     * 视频
     */
//    private VideoView video;

    /**
     * 视频封面图片
     */
    private ImageView videoPicture;

    /**
     * 请求参数
     */
    private RequestParams params;
    /**
     * 标题图片
     */
    private ImageView titlePicture;
    /**
     * 名字
     */
    private TextView name;
    /**
     * 价格
     */
    private TextView price;
    /**
     * 特色
     */
    private TextView feature;
    /**
     * 描述简介
     */
    private TextView descaribe;
    /**
     * 买断流程
     */
    private TextView process;
    /**
     * 评价
     */
    private TextView evaluate;
    /**
     * 声明
     */
    private TextView statement;
    /**
     * 欢迎众筹买断
     */
    private TextView HuanYingZhongChouMaiDuan;
    /**
     * 申请
     */
    private TextView apply;
    /**
     * 申请报名
     */
    private Button signUp;
    private int esotericaId;

    public String urlV = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_esoterica_details_d);
        initView();
        setOnClickListener();



    }

    /**
     * 初始化view
     */
    private void initView() {
        Intent intent = getIntent();

        //标题设置
        String name = intent.getStringExtra("name");
        esotericaId = intent.getIntExtra("id", -1);
        setTitleMore(View.VISIBLE, View.INVISIBLE, name, null, null, null);
        setBackArrows(findViewById(R.id.bar_top_conver_classify_back));

        //控件初始化
//        video = (VideoView) findViewById(R.id.vi_find_esoterica_details_video);
        videoPicture = (ImageView) findViewById(R.id.im_find_esoterica_details_videoPicture);
        this.titlePicture = (ImageView) findViewById(R.id.im_find_esoterica_details_picture);
        this.name = (TextView) findViewById(R.id.tv_find_esoterica_details_name);
        this.price = (TextView) findViewById(R.id.tv_find_esoterica_details_price);
        this.feature = (TextView) findViewById(R.id.tv_find_esoterica_details_feature);
        this.descaribe = (TextView) findViewById(R.id.tv_find_esoterica_details_descarobe);
        this.process = (TextView) findViewById(R.id.tv_find_esoterica_details_process);
        this.evaluate = (TextView) findViewById(R.id.tv_find_esoterica_details_evaluate);
        this.statement = (TextView) findViewById(R.id.tv_find_esoterica_details_statement);
        this.apply = (TextView) findViewById(R.id.tv_find_esoterica_details_apply);
        this.HuanYingZhongChouMaiDuan = (TextView) findViewById(R.id.tv_find_esoterica_details_huanYingMaiDuan);
        this.signUp = (Button) findViewById(R.id.btn_find_esoterica_details_signUp);


        //调用接口
        params = new RequestParams();
        params.addBodyParameter("telphone", MySharedPreferences.getSharedPreferencesKeyAndValue(
                FindEsotericaDetails_D.this, MySharedPreferences.USER_DATA, "tlephone"));
        params.addBodyParameter("password", MySharedPreferences.getSharedPreferencesKeyAndValue(
                FindEsotericaDetails_D.this, MySharedPreferences.USER_DATA, "password"));
        params.addBodyParameter("id", esotericaId + "");
        getHttpDetails(HttpInterfaces.Miji_Show, params, esotericaId);


    }


    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        videoPicture.setOnClickListener(onClickListener);
        signUp.setOnClickListener(onClickListener);


    }

    /**
     * 处理监听
     */
    @SuppressWarnings("unused")
    private OnClickListener onClickListener = new OnClickListener() {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (v.getId()) {
                //跳转播放视频
                case R.id.im_find_esoterica_details_videoPicture:
                    intent.setClass(FindEsotericaDetails_D.this, VideoPlayActivity.class);
                    intent.putExtra("url", SystemTools.getHttpFileUrl(urlV));

                    startActivity(intent);


                    break;
                case R.id.btn_find_esoterica_details_signUp:
                    intent.setClass(FindEsotericaDetails_D.this, FindEsotericaSignUpActivity.class);
                    intent.putExtra("esotericaId", esotericaId);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                default:
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }


    private void getHttpDetails(String url, RequestParams params, int id) {
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params, new MyRequestCallBack<String>(this, "加载中...") {


            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {
                    JSONObject json = new JSONObject(responseInfo.result);
                    boolean request = json.getBoolean("Result");

                    if (request) {
                        JSONObject model = json.getJSONObject("model");

//                        MyLog.i("text", "555*****" + model.getString("img_url"));

                        //给设置封面
                        bitmapUtils.display(titlePicture, SystemTools.getHttpFileUrl(model.getString("img_url")));

                        bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_play);
                        bitmapUtils.display(videoPicture, SystemTools.getHttpFileUrl(model.getString("guochengtupian")));


//                        MyLog.i("text", "555*****" + model.getString("img_url") +
//                                        "\ntitle**" + model.getString("title") +
//                                        "\nprice**" + model.getInt("price")
//                                        + "\njianjie**" + model.getString("jianjie")
//                                        + "\ngongyiliucheng**" + model.getString("gongyiliucheng")
//                                        + "\nmaiduanshengming**" + model.getString("maiduanshengming")
//                        );


                        name.setText(model.getString("title").toString());
                        price.setText(model.getInt("price") + "");
                        feature.setText(model.getString("tedian"));
                        descaribe.setText(model.getString("jianjie"));
                        evaluate.setText(model.getString("pingjia").toString());
                        process.setText(model.getString("gongyiliucheng").toString());
                        statement.setText(model.getString("maiduanshengming").toString());
                        HuanYingZhongChouMaiDuan.setText(model.getString("huanyingzongchoumaiduan").toString());

                        //获取视频
                        urlV = model.getString("liuchengshipin");

                        Uri uri = Uri.parse(HttpInterfaces.ORG + urlV);

                        Bitmap bm=getVideoThumbnail(uri.toString(), 50, 50, 2);

                        MyLog.e("text", "***"+bm == null ? "yes" : "no"+"****"+uri.toString());

//                        videoPicture.setImageBitmap(bm);
//                        titlePicture.setImageBitmap(bm);

                    } else {
                        MyToast.show(FindEsotericaDetails_D.this, "服务器忙，请稍后重试..", 1);
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }


            }
        });


    }




    /**
     * 获取视频缩略图
     * @param videoPath
     * @param width
     * @param height
     * @param kind
     * @return
     */
    private Bitmap getVideoThumbnail(String videoPath, int width , int height, int kind){
        Bitmap bitmap = null;
        bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);


        return bitmap;
    }


}
